package com.skill.shareskill.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.skill.shareskill.R;
import com.skill.shareskill.activity.DetailActivityActivity;
import com.skill.shareskill.activity.LoginActivity;
import com.skill.shareskill.adapter.ActivityItemAdapter;
import com.skill.shareskill.bean.ActivityItem;
import com.skill.shareskill.service.AttentActivityService;
import com.skill.shareskill.service.LoadDataThread;
import com.skill.shareskill.utils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/2/1 0001.
 */

public class FindAttentionFragment extends Fragment {
    private ListView lv_find_attention;
    private Gson gson=new Gson();
    private ActivityItemAdapter adapter;
    private List<HashMap<String, Object>> bdata;
    private List<ActivityItem> activityItemList;
    private String activityUrl;
    private String name;
    private String price;
    private String no;
    private String img;
    private  String aname="aname";
    private String aprice="aprice";
    private String aimg="aimg";
    private String ano="ano";
   /* private Handler handler=new Handler(){
        public void handleMessage(Message message){
            String activityJson= (String) message.obj;
            Log.d("network", "sb==>" + activityJson);
            activityItemList=gson.fromJson(activityJson, new TypeToken<List<ActivityItem>>(){}.getType());
            if(adapter==null){
                adapter=new ActivityItemAdapter(getActivity(),activityItemList);
                lv_find_attention.setAdapter(adapter);
            }else{
                adapter.notifyDataSetChanged();
            }
        }

    };*/
   /* @Override
   public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lv_find_attention=getActivity().findViewById(R.id.lv_find_attention);
        //添加ListView点击事件
        lv_find_attention.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String text=(String)((TextView) view.findViewById(R.id.tv_title)).getText();
                Toast.makeText(getActivity(),text,Toast.LENGTH_LONG).show();
                Intent intent=new Intent(getActivity(), DetailActivityActivity.class);
                startActivity(intent);
            }
        });
        activityUrl= HttpUtils.BASE_PATH+"activity/queryAllActivity";
        LoadDataThread lat=new LoadDataThread(handler,activityUrl);
        lat.start();
    }
*/
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

       View v=inflater.inflate(R.layout.fragment_find_attention,container,false);
        lv_find_attention= (ListView) v.findViewById(R.id.lv_find_attention);
//        String activityUrl= HttpUtils.BASE_PATH+"activity.json";
//        LoadDataThread lat=new LoadDataThread(handler,activityUrl);
//        lat.start();
        new Thread(){
            public void run() {
                final String result = AttentActivityService.AttentActivityByClientPost();
                if (result != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                //JSON字符串

                                //根据JSON字符串 获得JSON object对象
                              /*  JSONObject jsonObject = new JSONObject(result);
                                //根据key 获取相应的value
                                String errno = jsonObject.getString("errno");
                                String errmsg="年龄:" + jsonObject.getString("errmsg");*/
                                JSONArray data = new JSONArray(result);


                                bdata = new ArrayList<HashMap<String, Object>>();
                                for (int i = 0; i < data.length(); i++) {
                                    HashMap<String, Object> mapItem = new HashMap<String, Object>();
                                    JSONObject jsonObject2 = (JSONObject) data.get(i);
                                    name = jsonObject2.getString("actName");
                                    price = jsonObject2.getString("actPrice");
                                    no = jsonObject2.getString("actNo");
//                                    img = jsonObject2.getString("actImg");
                                    mapItem.put(aname, name);
                                    mapItem.put(aprice, price);
                                    mapItem.put(ano, no);
                                    mapItem.put(aimg, R.drawable.pic1);
                                    bdata.add(mapItem);
                                    System.out.println("结果"+bdata);
                                }
                                SimpleAdapter simAdapter = new SimpleAdapter(
                                        getActivity(), bdata, R.layout.list_activity,
                                        new String[] {  aname,aprice,aimg}, new int[] {
                                        R.id.tv_ana,R.id.tv_apr,R.id.iv_aim });
                                lv_find_attention.setAdapter(simAdapter);


                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity().getApplicationContext(), "结果" + result, Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }

        }.start();
        lv_find_attention.setOnItemClickListener(new itemClick());
        return v;

    }
    class itemClick implements OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            Map<String, String> infoMap = (Map<String, String>) parent.getItemAtPosition(position);
            String aname=infoMap.get("aname");
            String aprice=infoMap.get("aprice");
            System.out.println(infoMap.get("aname"));
            System.out.println(infoMap.get("aprice"));
            Toast.makeText(getActivity().getApplicationContext(), "你点击了" + name+"活动", Toast.LENGTH_LONG).show();
            Toast.makeText(getActivity().getApplicationContext(), "后台接口还没写好", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivityActivity.class);
            Bundle b = new Bundle();
            b.putString("name", aname);
            b.putString("price", aprice);
            intent.putExtras(b);
            getActivity().startActivity(intent);
        }

    }
}
